package cn.wolfcode.luowowo.mgrsite.controller;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.StrategyTag;
import cn.wolfcode.luowowo.article.service.IStrategyCatalogService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.article.service.IStrategyThemeService;
import cn.wolfcode.luowowo.common.query.QueryObject;
import cn.wolfcode.luowowo.common.query.StrategyQuery;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/strategyDetail")
public class StrategyDetailController {
    @Reference
    private IStrategyDetailService detailService;
    @Reference
    private IStrategyCatalogService catalogService;
    @Reference
    private IStrategyThemeService themeService;
    @Reference
    private IStrategyTagService tagService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") StrategyQuery qo, Model model){
        model.addAttribute("pageInfo", detailService.query(qo));
        return "strategyDetail/list";
    }

    @RequestMapping("/input")
    public String input(Long id, Model model){
        //id不为null表示编辑,查询出攻略还有tags共享
        if (id != null){
            model.addAttribute("entity", detailService.selectContainContentById(id));
            model.addAttribute("tags", tagService.selectTagsAsStringByDetailId(id));
        }
        //共享catalogs和themes用于下拉框
        model.addAttribute("catalogs", catalogService.selectAll());
        model.addAttribute("themes", themeService.selectAll());
        return "strategyDetail/input";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(StrategyDetail detail, String tags){
        //调用方法维护标签tag和文章detail的关系
        tagService.saveByString(tags);
        //saveOrUpdate返回值即为AjaxResult
        return detailService.saveOrUpdate(detail, tags);
    }
}
